package com.adee.algorithm.utils;

import java.util.Arrays;
import java.util.Random;
import java.util.stream.Collectors;

public class Util {
    // 打印数组
    public static void print(int[] arr) {
        if (arr == null) {
            System.out.println("null");
        } else {
            String str = Arrays.stream(arr).mapToObj(i -> i + "").collect(Collectors.joining(" "));
            System.out.println(str);
        }
    }

    public static void print(String msg, int[] arr) {
        System.out.println(msg);
        if (arr == null) {
            System.out.println("null");
        } else {
            String str = Arrays.stream(arr).mapToObj(i -> i + "").collect(Collectors.joining(" "));
            System.out.println(str);
        }
    }

    // 生成一个长度为n的 int数组
    public static int[] genArr(int n) {
        Random r = new Random(1);
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = r.nextInt(100);
        }
        return arr;
    }

    // 降序排序
    public static void sort(int[] arr) {
        Arrays.sort(arr);
    }

    // 升序排序
    public static void sortR(int[] arr) {
        Arrays.sort(arr);
        int len = arr.length;
        int tmp;
        for (int i = 0; i < len/2; i++) {
            tmp = arr[i];
            arr[i] = arr[len-1-i];
            arr[len-1-i] = tmp;
        }
    }

    public static void main(String[] args) {
        print(new int[]{8, 10, 1, 5, 20, 9});
        print(genArr(20));
    }
}
